package com.liguang.rcs.admin.web.user;

import com.liguang.rcs.admin.db.entity.DodmsUserEntity;
import com.liguang.rcs.admin.util.copy.BeanUtils;
import com.liguang.rcs.admin.util.copy.CopyProperty;
import com.liguang.rcs.admin.util.copy.EnableCopyProperties;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;

@Slf4j
@Data
@EnableCopyProperties
public class UserVO {

    @CopyProperty
    private Long id;

    @CopyProperty
    private String userName;

    @CopyProperty
    private String avatar = "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif";

    @CopyProperty
    private Long[] roleIds;

    private String password;

    private Set<String> roles;

    private Set<String> perms;

    /**
     * 通过entity生成vo对象
     */
    public static UserVO buildFrom(DodmsUserEntity entity) {
        try {
            UserVO vo = new UserVO();
            BeanUtils.reverseCopyProperties(vo, entity);
            return vo;
        } catch (Exception ex) {
            log.warn("[Account] build UserVO fail, Exception:{}", ex);
            return null;
        }
    }

    public DodmsUserEntity toEntity() {
        try {
            DodmsUserEntity entity = new DodmsUserEntity();
            BeanUtils.copyProperties(this, entity);
            return entity;
        } catch (Exception ex) {
            log.warn("[Account] build DodmsUserEntity fail, Exception:{}", ex);
            return null;
        }
    }
}
